package com.ysg.utils;

import android.Manifest;
import android.util.Log;
import androidx.fragment.app.FragmentActivity;
import com.ysg.utils.permission.RxPermissions;

/**
 * 权限工具类
 */
public class YPermissionUtil {

    public interface OnPermissionListener{
        void onSuccess();
        void onError();
    }

//    Manifest.permission.ACCESS_FINE_LOCATION,
//    Manifest.permission.ACCESS_COARSE_LOCATION,
//    Manifest.permission.CAMERA,
//    Manifest.permission.READ_PHONE_STATE,
//    Manifest.permission.RECORD_AUDIO,
//    Manifest.permission.READ_EXTERNAL_STORAGE,
//    Manifest.permission.WRITE_EXTERNAL_STORAGE)

//    YDialogUtil.getInstance().showConfirm(activity, "").setOnConfirmListener(()->{
//
//    });
    /**
     * 读写权限
     */
    public static void requestStorage(FragmentActivity activity, String content, OnPermissionListener listener){
        boolean b = YSPUtils.getInstance().getBoolean("permission_storage", true);
        if(b){
            YDialogUtil.getInstance().showConfirm(activity, content).setOnConfirmListener(()->{
                YSPUtils.getInstance().put("permission_storage", false);

                new RxPermissions(activity)
                        .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                        .subscribe(granted->{
                            Log.e("granted", ""+granted);
                            if (granted){
                                if (listener != null){
                                    listener.onSuccess();
                                }
                            }else{
                                YToastUtil.showShort("拒绝了读写权限，导致功能无法正常使用");
                            }
                        });
            });
        }else{
            new RxPermissions(activity)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(granted->{
                        Log.e("granted", ""+granted);
                        if (granted){
                            if (listener != null){
                                listener.onSuccess();
                            }
                        }else{
                            YToastUtil.showShort("拒绝了读写权限，导致功能无法正常使用");
                        }
                    });
        }
    }

    /**
     * 定位权限
     */
    public static void requestLocation(FragmentActivity activity, String content, OnPermissionListener listener){
        boolean b = YSPUtils.getInstance().getBoolean("permission_location", true);
        if (b){
            YDialogUtil.getInstance().showConfirm(activity, content).setOnConfirmListener(()->{
                YSPUtils.getInstance().put("permission_location", false);

                new RxPermissions(activity)
                        .request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                        .subscribe(granted->{
                            Log.e("granted", ""+granted);
                            if (granted){
                                if (listener != null){
                                    listener.onSuccess();
                                }
                            }else{
                                YToastUtil.showShort("拒绝了定位权限，导致功能无法正常使用");
                            }
                        });
            });
        }else{
            new RxPermissions(activity)
                    .request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                    .subscribe(granted->{
                        Log.e("granted", ""+granted);
                        if (granted){
                            if (listener != null){
                                listener.onSuccess();
                            }
                        }else{
                            YToastUtil.showShort("拒绝了定位权限，导致功能无法正常使用");
                        }
                    });
        }
    }
    /**
     * 录音权限
     */
    public static void requestAudio(FragmentActivity activity, String content, OnPermissionListener listener){
        boolean b = YSPUtils.getInstance().getBoolean("permission_audio", true);
        if (b){
            YDialogUtil.getInstance().showConfirm(activity, content).setOnConfirmListener(()->{
                YSPUtils.getInstance().put("permission_audio", false);

                new RxPermissions(activity)
                        .request(Manifest.permission.RECORD_AUDIO)
                        .subscribe(granted->{
                            Log.e("granted", ""+granted);
                            if (granted){
                                if (listener != null){
                                    listener.onSuccess();
                                }
                            }else{
                                YToastUtil.showShort("拒绝了录音权限，导致功能无法正常使用");
                            }
                        });
            });
        }else{
            new RxPermissions(activity)
                    .request(Manifest.permission.RECORD_AUDIO)
                    .subscribe(granted->{
                        Log.e("granted", ""+granted);
                        if (granted){
                            if (listener != null){
                                listener.onSuccess();
                            }
                        }else{
                            YToastUtil.showShort("拒绝了录音权限，导致功能无法正常使用");
                        }
                    });
        }
    }

    /**
     * 拍照权限
     */
    public static void requestCamera(FragmentActivity activity, String content, OnPermissionListener listener){
        boolean b = YSPUtils.getInstance().getBoolean("permission_camera", true);
        if (b){
            YDialogUtil.getInstance().showConfirm(activity, content).setOnConfirmListener(()->{
                YSPUtils.getInstance().put("permission_camera", false);

                new RxPermissions(activity)
                        .request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                        .subscribe(granted->{
                            Log.e("granted", ""+granted);
                            if (granted){
                                if (listener != null){
                                    listener.onSuccess();
                                }
                            }else{
                                YToastUtil.showShort("拒绝了拍照权限，导致功能无法正常使用");
                            }
                        });
            });
        }else{
            new RxPermissions(activity)
                    .request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(granted->{
                        Log.e("granted", ""+granted);
                        if (granted){
                            if (listener != null){
                                listener.onSuccess();
                            }
                        }else{
                            YToastUtil.showShort("拒绝了拍照权限，导致功能无法正常使用");
                        }
                    });
        }


    }

    /**
     * 实名认证
     */
    public static void requestFace(FragmentActivity activity, OnPermissionListener listener){
        new RxPermissions(activity)
                .request(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
                .subscribe(granted->{
                    if (granted){
                        if (listener != null){
                            listener.onSuccess();
                        }
                    }else{
                        YToastUtil.showShort("拒绝了权限，导致功能无法正常使用");
                    }
                });


    }



}
